﻿<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Title="Register" Inherits="System.Web.Mvc.ViewPage" %>
<asp:Content ID="content" ContentPlaceHolderID="MainContent" runat="server">

	<h2>Register</h2>

	<% if( ( TempData["ErrorMessage"] ?? ViewData["ErrorMessage"] ) != null ){ %>
	<p class="MvcMembershipError"><% =(string)TempData["ErrorMessage"] ?? (string)ViewData["ErrorMessage"]%></p>
	<% } %>

	<% using(Html.Form( "FormsAuthentication", "CreateUser" )){ %>
	<fieldset class="MvcMembership">

		<legend>Register</legend>

		<div><label for="userName">User Name:</label> <% =Html.TextBox("userName", new { size = 32, maxlength = 256 })%></div>
		<div><label for="emailAddress">Email Address:</label> <% =Html.TextBox("emailAddress", new { size = 32, maxlength = 256 })%></div>
		<hr />
		<div><label for="password">Password:</label> <% =Html.Password("password", new { size = 32 })%></div>
		<div><label for="passwordConfirm">Confirm Password:</label> <% =Html.Password("passwordConfirm", new { size = 32})%></div>
		<% =Html.Hidden( "returnUrl", Request.QueryString["ReturnUrl"] ?? FormsAuthentication.DefaultUrl ) %>

		<% if (ViewData["RequiresQuestionAndAnswer"] != null && (bool)ViewData["RequiresQuestionAndAnswer"] == true) { %>
		<hr />
		<div><label for="passwordQuestion">Password Question:</label> <% =Html.TextBox("passwordQuestion", new { size = 32, maxlength = 256 })%></div>
		<div><label for="passwordAnswer">Password Answer:</label> <% =Html.TextBox("passwordAnswer", new { size = 32, maxlength = 128 })%></div>
		<% } %>

		<div><% =Html.SubmitButton( "submit", "Register", new { onclick = "return  starterKit_mvc_membership_validateRegister();" } )%></div>

	</fieldset>
	<% } %>

	<script type="text/javascript">
	/* <![CDATA[ */

		function starterKit_mvc_membership_validateRegister()
		{

			var pwd_minChars = <% =(int)ViewData["MinimumPasswordLength"] %>;
			var pwd_minNonAlphaChars = <% =(int)ViewData["MinimumNonAlphaNumericCharacters"] %>;

			var userName = document.getElementById('userName').value.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
			if( userName.length < 1 )
			{
				alert( 'Please enter your user name.' );
				document.getElementById('userName').focus();
				return false;
			}

			var emailAddress = document.getElementById('emailAddress').value.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
			if( emailAddress.length < 1 )
			{
				alert( 'Please enter your email address.' );
				document.getElementById('emailAddress').focus();
				return false;
			}
			//from: http://www.douglaskarr.com/2007/10/28/javascript-regex-emailaddress/
			var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
			if( !emailRegEx.test(emailAddress) )
			{
				alert( 'Please provide a valid email address.' );
				document.getElementById('emailAddress').focus();
				return false;
			}

			var password = document.getElementById('password').value.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
			if( password.length < 1 )
			{
				alert( 'Please enter your password.' );
				document.getElementById('password').focus();
				return false;
			}
			if( password.length < pwd_minChars )
			{
				alert( 'Your password must be at least ' + pwd_minChars + ' characters long.' );
				document.getElementById('password').focus();
				return false;
			}
			var nonAlphaChars = password.replace( /[a-zA-Z0-9]*/, '' );
			if( nonAlphaChars.length < pwd_minNonAlphaChars )
			{
				alert( 'Your password must contain at least ' + pwd_minNonAlphaChars + ' non alphanumeric characters.' );
				document.getElementById('password').focus();
				return false;
			}

			var passwordConfirm = document.getElementById('passwordConfirm').value.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
			if( passwordConfirm.length < 1 )
			{
				alert( 'Please confirm your password.' );
				document.getElementById('passwordConfirm').focus();
				return false;
			}
			if( password != passwordConfirm )
			{
				alert( 'Please make sure what you entered in to the "Confirm Password" textbox matches the password you are trying to create.' );
				document.getElementById('password').focus();
				return false;
			}

			<% if (ViewData["RequiresQuestionAndAnswer"] != null && (bool)ViewData["RequiresQuestionAndAnswer"] == true) { %>
			var passwordQuestion = document.getElementById('passwordQuestion').value.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
			if( passwordQuestion.length < 1 )
			{
				alert( 'Please enter your password question.' );
				document.getElementById('passwordQuestion').focus();
				return false;
			}
			var passwordAnswer = document.getElementById('passwordAnswer').value.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
			if( passwordAnswer.length < 1 )
			{
				alert( 'Please enter your password answer.' );
				document.getElementById('passwordAnswer').focus();
				return false;
			}
			<% } %>

			return true;

		}

	/* ]]> */
	</script>


</asp:Content>
